//
//  XYTConsolidatedStatementViewController.m
//  XYTPayOrderSystem
//
//  Created by wupeng on 2017/8/23.
//  Copyright © 2017年 wupeng. All rights reserved.
//

#import "XYTConsolidatedStatementViewController.h"
#import "XYTStatementView.h"
#import "XYTStatementModel.h"

@interface XYTConsolidatedStatementViewController ()

@property (nonatomic, copy) NSString *createDate;

@property (nonatomic, strong) XYTStatementView *amountView;

@property (nonatomic, strong) XYTStatementView *moneyView;

@end

@implementation XYTConsolidatedStatementViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"汇总报表";
    
    [self setupUI];
    
    
}

- (void)setupUI{
    
    UIDatePicker *datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(10,75,kScreen_Width-10*2-75,60)];
    datePicker.datePickerMode = UIDatePickerModeDate;
    [datePicker addTarget:self action:@selector(dateChanged:) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:datePicker];
    
    UIButton *queryBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    queryBtn.frame = CGRectMake(CGRectGetMaxX(datePicker.frame)+35, 85, 40, 40);
    [queryBtn.layer setBorderWidth:0.8];
    [queryBtn.layer setBorderColor:[UIColor lightGrayColor].CGColor];
    [queryBtn.layer setCornerRadius:5.0];
    [queryBtn setImage:[UIImage imageNamed:@"searchscope"] forState:UIControlStateNormal];
    [queryBtn addTarget:self action:@selector(onClickOrderQuery) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:queryBtn];
    
    XYTStatementView *amountView = [[XYTStatementView alloc] initWithFrame:CGRectMake(15, 170, kScreen_Width - 15*2, 95)];
    amountView.imgView.image = [UIImage imageNamed:@"orderSum"];
    amountView.titleLab.text = @"成功订单总数:";
    amountView.dateLab.text = @"0";
    [self.view addSubview:amountView];
    self.amountView = amountView;
    
    XYTStatementView *moneyView = [[XYTStatementView alloc] initWithFrame:CGRectMake(15, CGRectGetMaxY(amountView.frame)+15, kScreen_Width - 15*2, 95)];
    moneyView.imgView.image = [UIImage imageNamed:@"orderMoney"];
    moneyView.titleLab.text = @"成功订单总金额:";
    moneyView.dateLab.text = @"¥ 0.00";
    [self.view addSubview:moneyView];
    self.moneyView = moneyView;
}

- (void)loadData{
    [UIView showHUD:nil];
    NSString *createDate = self.createDate;
    NSString *signStr = [NSString stringWithFormat: @"%@%@%@",AppKey,AppSecret,createDate];
    NSString *md5Sign = [Md5Util md5:signStr];
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params xyt_setObj:createDate forKey:@"createDate"];
    [params xyt_setObj:md5Sign forKey:@"sign"];
    XYTWeakSelf(self)
    [XYTNetRequest POST:StatementUrl parameters:params isNeedKey:NO success:^(id responseObject) {
        XYTStrongSelf(weakSelf)
        [UIView hideHUD];
        XYTStatementModel *model = [XYTStatementModel mj_objectWithKeyValues:responseObject];
        strongSelf.amountView.dateLab.text = [NSString stringWithFormat:@"%ld",(long)model.orderNum];
        strongSelf.moneyView.dateLab.text = [NSString stringWithFormat:@"¥ %.2f",model.orderMoney];
    } failure:^(NSString *errorMsg) {
        XYTStrongSelf(weakSelf)
        [UIView hideHUD];
        [strongSelf showAlertWithTitle:errorMsg];
        
    }];
}

- (void)dateChanged:(id)sender{
    UIDatePicker *datePicker = (UIDatePicker *)sender;
    NSDate *theDate = datePicker.date;
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    dateFormatter.dateFormat = @"YYYY-MM-dd";
    self.createDate = [dateFormatter stringFromDate:theDate];
    NSLog(@"选择的日期:%@",self.createDate);
}

- (void)onClickOrderQuery{
    [self loadData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
